package com.example.design.state;

import com.example.design.state.event.Event;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 状态机.
 *
 * @author duk
 * @date 2024-01-15 13:56
 */
@Slf4j
@Component
public class StateMachine {

    @Resource
    private Map<String, Event> eventMap;

    public void trigger(Context ctx) {
        Event event = eventMap.get(ctx.getEventType().getEventService());
        if (event != null) {
            event.beforeTransit(ctx);
            event.transit(ctx);
            event.postTransit(ctx);
        }
    }

}
